#include <gtest/gtest.h>
#include "../mqserver/mq_consumer.hpp"
mq_zxws::ConsumerManager::ptr cmp;
class Consumertest:public testing::Environment{
public:
    void SetUp() override{
        cmp = std::make_shared<mq_zxws::ConsumerManager>();
        cmp->initComsumerQueue("queue1");
        cmp->initComsumerQueue("queue2");
        cmp->initComsumerQueue("queue3");
    }
    void TearDown() override{
        cmp->clear();
    }
};
// using ConsumerCallBack = std::function<void(const std::string, const BasicProperties* bp, const std::string)>;

void cb(const std::string& ctag, const mq_zxws::BasicProperties* bp, const std::string & body){
    std::cout << ctag << " 消费了 ： " << body << std::endl;
}

TEST(Binding, insert){
    cmp->create("queue1", "consumer1", false, cb);
    cmp->create("queue1", "consumer2", false, cb);
    cmp->create("queue1", "consumer3", false, cb);

    ASSERT_EQ(cmp->exists("queue1", "consumer1"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer3"), true);
}

TEST(Binding, remove){
    cmp->remove("queue1", "consumer1");
    ASSERT_EQ(cmp->exists("queue1", "consumer1"), false);
    ASSERT_EQ(cmp->exists("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer3"), true);
}

TEST(Binding, choose){
    auto cp = cmp->choose("queue2");
    ASSERT_EQ(cp->qname, std::string("queue2"));
    ASSERT_EQ(cp->tag, std::string("consumer2"));

    cp = cmp->choose("queue2");
    ASSERT_EQ(cp->tag, std::string("consumer3"));

    cp = cmp->choose("queue2");
    ASSERT_EQ(cp->tag, std::string("consumer2"));
}


int main(int argc, char*argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new Consumertest);
    int res = RUN_ALL_TESTS();
    return 0;
}